package com.redlion.object;

import org.andengine.entity.scene.Scene;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.util.adt.pool.GenericPool;
import org.andengine.util.debug.Debug;

import com.badlogic.gdx.physics.box2d.FixtureDef;

public class ImpactPool extends GenericPool<AnimatedImpact> {
	private static final String TAG = "ImpactPool";
	public ITiledTextureRegion mTextureRegion;
	public FixtureDef mFixtureDef;
	private Scene mScene;

	public ImpactPool(ITiledTextureRegion tiledTextureRegion, FixtureDef fixtureDef, Scene scene) {
		if (tiledTextureRegion == null) {
			throw new IllegalArgumentException("The texture region must not be NULL");
		}
		if (fixtureDef == null) {
			throw new IllegalArgumentException("The fixture definition must not be NULL");
		}
		mTextureRegion = tiledTextureRegion;
		mFixtureDef = fixtureDef;
		mScene = scene;
		batchAllocatePoolItems(2);
	}

	/**
	 * Called when a Bullet is required but there isn't one in the pool
	 */
	@Override
	protected AnimatedImpact onAllocatePoolItem() {
		Debug.d(TAG, "onAllocatePoolItem");
		AnimatedImpact impact = new AnimatedImpact(Vector2Pool.obtain(), mTextureRegion, mFixtureDef);
		mScene.attachChild(impact.sprite);
		impact.deactivate();
		return impact;
	}

	/**
	 * Called when a Bullet is sent to the pool
	 */
	@Override
	protected void onHandleRecycleItem(final AnimatedImpact impact) {
		Debug.d(TAG, "onHandleRecycleItem");
		impact.deactivate();
	}

	/**
	 * Called just before a Bullet is returned to the caller, this is where you
	 * write your initialize code i.e. set location, rotation, etc.
	 */
	@Override
	protected void onHandleObtainItem(final AnimatedImpact impact) {
		Debug.d(TAG, "onHandleObtainItem");
		impact.activate();
	}
}
